/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package customer_jdom;

import appClientHandler.Output;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * eCOM - Adopte un Presta - Mars-Avril 2013
 * @author Kheira, Jef, William, Tuan-Long
 */
public abstract class AbstractJDOM<T> {
    public void print(PrintStream out, List<T> entitys, String output) 
    {
        if(output != null && output.equals(Output.XML))
        {
                printXML(out, entitys);

        }else{
            print(out, entitys);
        }
    }


    public void print(PrintStream out, T entity, String output)
    {
        if (output != null && output.equals(Output.XML))
        {
                printXML(out, entity);

        } else
        {
            List<T> entitys = new ArrayList<T>();
            entitys.add(entity);
            print(out, entitys);
        }
    }

    public void printXML(PrintStream out, List<T> entitys)
    {
        try 
        {
            XMLOutputter xMLOutputter = new XMLOutputter();
            xMLOutputter.output(getXmlRepresentation(entitys), out);

            out.println();
        }catch(Exception e){
            e.printStackTrace(out);
        }
    }




    public void printXML(PrintStream out, T entity)
    {
        try
        {
            XMLOutputter xMLOutputter = new XMLOutputter();
            xMLOutputter.output(getXmlRepresentation(entity), out);

            out.println();
        }
        catch(Exception e)
        {
            e.printStackTrace(out);
        }
    }


    public abstract void print(PrintStream out, List<T> entitys);

    public Element getXmlRepresentation(List<T> entitys)
    {
        Element element = new Element(getClassName() + "s");
        for (T entity : entitys)
        {
            element.addContent(getXmlRepresentation(entity));
        }
        return element;
    }

    public abstract Element getXmlRepresentation(T entity);

    public abstract String getClassName();

}
